//
//  MBHomePPVPopView.m
//  Meboo
//
//  Created by kyo on 2024/4/28.
//

#import "MBHomePPVPopView.h"
#import "MBHomePPVCell.h"
#import "KLPaymentManager.h"
@interface MBHomePPVPopView ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *typeImageView;
@property (nonatomic, strong) UIImageView *emojiImageView;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UIButton *continueBtn;

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *subTitleLab;

@property (nonatomic , strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;

//product
@property (nonatomic, strong) NSMutableArray *productIDArr;
@property (nonatomic, copy) NSString *productID;
@property (nonatomic, assign) NSInteger buyNum;
@end

@implementation MBHomePPVPopView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        self.buyNum = 5;
    }
    return self;
}

#pragma mark - Public Method
- (void)setIsVideo:(BOOL)isVideo {
    _isVideo = isVideo;
    [self.dataArr removeAllObjects];
    if (isVideo) {
        self.titleLab.text = @"Creator-Inspiration video";
        self.typeImageView.image = ImageWithName(@"home_pop_pv_icon");
        self.emojiImageView.image = ImageWithName(@"home_pop_pv_emoji");
        self.productID = @"meboo_video_ios_5";
        self.buyNum = 5;
        
        NSMutableDictionary *dict4 = [NSMutableDictionary dictionary];
        [dict4 setObject:@"$ 4.99" forKey:@"money"];
        [dict4 setObject:@"1 Inspiration video" forKey:@"title"];
        [dict4 setObject:@"0" forKey:@"isSelected"];
        
        NSMutableDictionary *dict5 = [NSMutableDictionary dictionary];
        [dict5 setObject:@"$ 14.99" forKey:@"money"];
        [dict5 setObject:@"5 Inspiration video" forKey:@"title"];
        [dict5 setObject:@"1" forKey:@"isSelected"];
        
        NSMutableDictionary *dict6 = [NSMutableDictionary dictionary];
        [dict6 setObject:@"$ 29.99" forKey:@"money"];
        [dict6 setObject:@"10 Inspiration video" forKey:@"title"];
        [dict6 setObject:@"0" forKey:@"isSelected"];
        
        [self.dataArr addObject:dict4];
        [self.dataArr addObject:dict5];
        [self.dataArr addObject:dict6];
        [self.continueBtn setBackgroundColor:HEXS(@"FF7EF2")];
    } else {
        self.titleLab.text = @"Creator-Inspiration image";
        self.typeImageView.image = ImageWithName(@"home_pop_pp_icon");
        self.emojiImageView.image = ImageWithName(@"home_pop_pp_emoji");
        self.productID = @"meboo_image_ios_5";
        self.buyNum = 5;
        
        NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
        [dict1 setObject:@"$ 2.99" forKey:@"money"];
        [dict1 setObject:@"1 Inspiration image" forKey:@"title"];
        [dict1 setObject:@"0" forKey:@"isSelected"];
        
        NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
        [dict2 setObject:@"$ 9.99" forKey:@"money"];
        [dict2 setObject:@"5 Inspiration image" forKey:@"title"];
        [dict2 setObject:@"1" forKey:@"isSelected"];
        
        NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];
        [dict3 setObject:@"$ 19.99" forKey:@"money"];
        [dict3 setObject:@"10 Inspiration image" forKey:@"title"];
        [dict3 setObject:@"0" forKey:@"isSelected"];
        [self.dataArr addObject:dict1];
        [self.dataArr addObject:dict2];
        [self.dataArr addObject:dict3];
        [self.continueBtn setBackgroundColor:HEXS(@"EE42FF")];
    }
    [self.collectionView reloadData];
}


#pragma mark - Action
- (void)closeAction {
    self.hidden = YES;
}

- (void)nextAction {
    __WeakSelf
    MBUserAccount *account = [MBUserAccount defaultAccount];
    self.backView.userInteractionEnabled = NO;
    [[KLPaymentManager sharePaymentManager] payWithProductID:self.productID];
    [KLPaymentManager sharePaymentManager].payBlock = ^(BOOL payState) {
        self.backView.userInteractionEnabled = YES;
        if (payState) {
            [MBProgressHUD showSuccessMessage:@"Pay Success"];
            weakSelf.hidden = YES;
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.isVideo) {
                    account.checkPVNum += self.buyNum;
                } else {
                    account.checkPPNum += self.buyNum;
                }
                [MBUserAccount saveAccount:account];
                if (self.payBlock) {
                    self.payBlock(payState);
                }
            });
        }
    };
}

#pragma mark -- collectionView  delegate datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MBHomePPVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MBHomePPVCell" forIndexPath:indexPath];
    cell.dict = self.dataArr[indexPath.row];
    return cell;
}

//设置每个section的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(MBHeightScale(0), MBWidthScale(0), MBHeightScale(0), MBWidthScale(0));
}

//设置列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return MBHeightScale(8);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return MBWidthScale(0);
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ScreenWidth-MBWidthScale(78+56),MBHeightScale(61));
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        self.buyNum = 1;
    } else if (indexPath.row == 1) {
        self.buyNum = 5;
    } else {
        self.buyNum = 10;
    }
    for (int i = 0; i<self.dataArr.count; i++) {
        NSMutableDictionary *dict = self.dataArr[i];
        if (i == indexPath.row) {
            [dict setObject:@"1" forKey:@"isSelected"];
        } else {
            [dict setObject:@"0" forKey:@"isSelected"];
        }
        [self.dataArr replaceObjectAtIndex:i withObject:dict];
    }
    [self.collectionView reloadData];
    
    if (self.isVideo) {
        self.productID = self.productIDArr[indexPath.row+3];
    } else {
        self.productID = self.productIDArr[indexPath.row];
    }
    
}

#pragma mark - UI
- (void)setupUI {
    self.hidden = YES;
    self.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5];
    
    [self addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(39));
//        make.height.mas_equalTo((ScreenWidth-MBHeightScale(78))/297*482);
        make.center.equalTo(self);
    }];
    
    [self.backView addSubview:self.typeImageView];
    [self.typeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.mas_equalTo(MBHeightScale(25));
        make.width.height.mas_offset(MBHeightScale(68));
    }];
    
    [self.backView addSubview:self.emojiImageView];
    [self.emojiImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.typeImageView.mas_right).offset(MBHeightScale(-5));;
        make.centerY.equalTo(self.typeImageView.mas_bottom).offset(MBHeightScale(-10));
        make.width.height.mas_offset(MBHeightScale(36));
    }];
    
    [self.backView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeImageView.mas_bottom).offset(MBHeightScale(15));
        make.left.mas_equalTo(MBWidthScale(20));
        make.right.mas_equalTo(MBWidthScale(-20));
    }];
    
    [self.backView addSubview:self.subTitleLab];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(MBHeightScale(6));
        make.left.mas_equalTo(MBWidthScale(20));
        make.right.mas_equalTo(MBWidthScale(-20));
    }];
    
    [self.backView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subTitleLab.mas_bottom).offset(MBHeightScale(16));
        make.left.mas_equalTo(MBWidthScale(28));
        make.right.mas_equalTo(MBWidthScale(-28));
        make.height.mas_equalTo(MBHeightScale(200));
    }];
    
    [self.backView addSubview:self.continueBtn];
    [self.continueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(28));
        make.right.mas_equalTo(MBWidthScale(-28));
        make.height.mas_equalTo(MBHeightScale(37));
        make.top.equalTo(self.collectionView.mas_bottom).offset(MBHeightScale(30));
        make.bottom.mas_equalTo(MBHeightScale(-20));
    }];
    
    [self.backView addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backView.mas_right).offset(MBWidthScale(-15));
        make.top.equalTo(self.backView.mas_top).offset(MBWidthScale(20));
        make.width.height.mas_offset(MBHeightScale(18));
    }];
    
    [self.backView layoutIfNeeded];
    NSLog(@"&*&*&*%f,%f",self.backView.bounds.size.width,self.backView.bounds.size.height);
        [self.backView addSubview:self.imageView];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.backView);
            make.width.mas_equalTo(self.backView.bounds.size.width);
            make.height.mas_equalTo(self.backView.bounds.size.height);
        }];
    [self.backView sendSubviewToBack:self.imageView];
}

#pragma mark - Setter && Getter

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.numberOfLines = 0;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.text = @"Creator-Inspiration image";
        _titleLab.font = PDBoldScale(16);
        _titleLab.textColor = HEXS(@"ffffff");
    }
    return _titleLab;
}

- (UILabel *)subTitleLab {
    if (!_subTitleLab) {
        _subTitleLab = [UILabel new];
        _subTitleLab.numberOfLines = 0;
        _subTitleLab.textAlignment = NSTextAlignmentCenter;
        _subTitleLab.text = @"Support inspiring creators by offering paid access to their content. Let your support be their reward!";
        _subTitleLab.font = PDRegularScale(13);
        _subTitleLab.textColor = HEXS(@"ffffff");
    }
    return _subTitleLab;
}

- (UIButton *)continueBtn {
    if (!_continueBtn) {
        _continueBtn = [UIButton new];
        [_continueBtn setBackgroundColor:HEXS(@"EE42FF")];
        _continueBtn.layer.cornerRadius = MBHeightScale(37/2);
        [_continueBtn setTitle:@"Continue" forState:0];
        [_continueBtn setTitleColor:HEXS(@"ffffff") forState:0];
        _continueBtn.titleLabel.font = PDRegularScale(16);
        [_continueBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _continueBtn;
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton new];
        [_closeBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_closeBtn setBackgroundImage:ImageWithName(@"flash_chat_close") forState:0];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [UIView new];
        _backView.backgroundColor = UIColor.clearColor;
    }
    return _backView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [UIImageView new];
        _imageView.userInteractionEnabled = YES;
        _imageView.image = ImageWithName(@"home_pop_pppv");
    }
    return _imageView;
}

- (UIImageView *)typeImageView {
    if (!_typeImageView) {
        _typeImageView = [UIImageView new];
        _typeImageView.image = ImageWithName(@"home_pop_pp_icon");
    }
    return _typeImageView;
}

- (UIImageView *)emojiImageView {
    if (!_emojiImageView) {
        _emojiImageView = [UIImageView new];
        _emojiImageView.image = ImageWithName(@"home_pop_pp_emoji");
    }
    return _emojiImageView;
}



- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[MBHomePPVCell class] forCellWithReuseIdentifier:@"MBHomePPVCell"];

    }
    return _collectionView;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (NSMutableArray *)productIDArr {
    if (!_productIDArr) {
        _productIDArr = [NSMutableArray arrayWithArray:@[@"meboo_fc_ios_1",@"meboo_image_ios_5",@"meboo_image_ios_10",@"meboo_fc_ios_3",@"meboo_video_ios_5",@"meboo_video_ios_10"]];
    }
    return _productIDArr;
}
@end
